<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}

if($_POST['sub']!=''){
    $subjectid=empty($_POST['subjectid'])?0:(int)$_POST['subjectid'];
    if(empty($subjectid)){
            jumpajax('300','没有选择调查主题！','','closeCurrent');    
    }
    $itemid=empty($_POST['itemid'])?0:(int)$_POST['itemid'];
	unset($_POST['id']);
	unset($_POST['sub']);
    
    $oplist=$_POST["oplist"];
    $opval=$_POST["opval"];
    $data=array('subjectid'=>$subjectid,'title'=>$_POST["subject"],"ismultiple"=>$_POST["ismultiple"]);
    $title=trim($_POST["subject"]);
    if(trim($title)==""){
            jumpajax('300','选项主题不能为空！','');
    }      
 	if($itemid==0){
	    $itemid=$ezhand->insert("vote_option_item",$data);
		
	}
	else{
	    $ezhand->update("vote_option_item",$data,'id='.$itemid);
	
	}
    if(!empty($oplist)){
        while(list($k,$v)=each($oplist)){
            $v=trim($v);
            if(!empty($v)){
            $d=array("subjectid"=>$subjectid,"option"=>$v,"itemid"=>$itemid);
            if(!empty($opval[$k])){
                    $opid=$ezhand->update("vote_option",$d,"optionid=".$opval[$k]);
            }else{
                $opid=$ezhand->insert("vote_option",$d);    
            }
            
            }else{
                if(!empty($opval[$k])){
                 $ezhand->delete("vote_option","optionid=".$opval[$k]);
                }
            }
        }
        
    }
    
    jumpajax('200','选项保存成功','','closeCurrent');
}
else{
    $subjectid=empty($_GET['subjectid'])?0:(int)$_GET['subjectid'];
    if(empty($subjectid)){
            jumpajax('300','没有选择调查主题！','','closeCurrent');    
    }

    $id=empty($_GET['id'])?0:(int)$_GET['id'];
    
    if($id==0){
	   $votedata=array();
	}
	else{
        $row=$ezhand->select("vote_option_item",'*','id="'.$id.'"');
        $votedata=$ezhand->select_all("vote_option","*","itemid=".$id."");
	}
}
?>